﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Web.Services;
public partial class Spread_API_A_Spreadlogon : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        string returnStr = string.Empty;
        string type = Request.Form["edit"];
        switch (type)
        {
            case "logon":
                {
                    returnStr = UserLogon();
                    break;
                }
            case "info":
                {
                    returnStr = GetuserInfo();
                    break;
                }
            case "infoupdate":
                {
                    returnStr = InfoUpdate();
                    break;
                }
            case "bankupdate":
                {
                    returnStr = BankUpdate();
                    break;
                }
            case "passwordupdate":
                {
                    returnStr = PasswordUpdate();
                    break;
                }
            case "logonout":
                {
                    returnStr = LogonOut();
                    break;
                }
            case "listbyyear":
                {
                    returnStr = SpreadListByYear();
                    break;
                }
            case "listbymonth":
                {
                    returnStr = SpreadListByMonth();
                    break;
                }
            case "url":
                {
                    returnStr = GetUrl();
                    break;
                }
            default: break;
        }
        Response.Write(returnStr);
    }
    protected string UserLogon()
    {
        string returnStr = string.Empty;
        if (CheckCode(Request.Form["c"].ToString()))
        {
            string userName = Request.Form["u"].ToString();
            string passWord = EncrypterMD5.MD5.Md5(Request.Form["p"].ToString(), 32);
            BLL.Spreader bll = new BLL.Spreader();
            bool flag = bll.SpreaderLogon(userName, passWord);
            if (flag)
            {
                returnStr = "{\"err\":\"t\"}";
                Session["SpreaderUsername"] = userName;
            }
            else
            {
                returnStr = "{\"err\":\"p\"}";
            }
        }
        else
        {
            returnStr = "{\"err\":\"c\"}";
        }
        return returnStr;
    }
    protected bool CheckCode(string codeText)
    {
        bool flag = false;
        if (codeText.ToLower() == Session["CheckCode"].ToString().ToLower())
            flag = true;
        return flag;
    }
    protected string GetuserInfo()
    {
        string userName = Session["SpreaderUsername"].ToString();
        StringBuilder dataStr = new StringBuilder();
        BLL.Spreader bll = new BLL.Spreader();
        string userId = bll.GetSpreadIdByName(userName);
        DataSet ds = bll.SpreadList(userId);
        dataStr.Append("{");
        dataStr.Append("\"qq\":\"" + ds.Tables[0].Rows[0]["Sqq"].ToString() + "\",");
        dataStr.Append("\"email\":\"" + ds.Tables[0].Rows[0]["Semail"].ToString() + "\",");
        dataStr.Append("\"tel\":\"" + ds.Tables[0].Rows[0]["Stel"].ToString() + "\",");
        dataStr.Append("\"name\":\"" + ds.Tables[0].Rows[0]["Sname"].ToString() + "\",");
        dataStr.Append("\"image\":\"" + ds.Tables[0].Rows[0]["Simage"].ToString() + "\",");
        dataStr.Append("\"bank\":\"" + ds.Tables[0].Rows[0]["Sbank"].ToString() + "\",");
        dataStr.Append("\"bankname\":\"" + ds.Tables[0].Rows[0]["Sbankname"].ToString() + "\",");
        dataStr.Append("\"bankcode\":\"" + ds.Tables[0].Rows[0]["Sbankcode"].ToString() + "\",");
        dataStr.Append("\"logontime\":\"" + ds.Tables[0].Rows[0]["Slogontime"].ToString() + "\",");
        dataStr.Append("\"logonip\":\"" + ds.Tables[0].Rows[0]["Slogonip"].ToString() + "\"");
        dataStr.Append("}");
        return dataStr.ToString();
    }
    protected string InfoUpdate()
    {
        string returnStr = string.Empty;
        string userName = Session["SpreaderUsername"].ToString();
        StringBuilder dataStr = new StringBuilder();
        BLL.Spreader bll = new BLL.Spreader();
        string userId = bll.GetSpreadIdByName(userName);
        MODEL.Spreader model = new MODEL.Spreader();
        model.SQQ = Request.Form["q"];
        model.SEMAIL = Request.Form["e"];
        model.STEL = Request.Form["t"];
        model.SID = userId;
        bool flag = bll.SpreaderUpdateInformation(model);
        if (flag)
        {
            returnStr = "true";
        }
        return returnStr;
    }
    protected string BankUpdate()
    {
        string returnStr = string.Empty;
        string userName = Session["SpreaderUsername"].ToString();
        StringBuilder dataStr = new StringBuilder();
        BLL.Spreader bll = new BLL.Spreader();
        string userId = bll.GetSpreadIdByName(userName);
        MODEL.Spreader model = new MODEL.Spreader();
        model.SBANK = Request.Form["b"];
        model.SBANKNAME = Request.Form["n"];
        model.SBANKCODE = Request.Form["c"];
        model.SID = userId;
        bool flag = bll.SpreaderUpdateBank(model);
        if (flag)
        {
            returnStr = "true";
        }
        return returnStr;
    }
    protected string PasswordUpdate()
    {
        string returnStr = string.Empty;
        string userName = Session["SpreaderUsername"].ToString();
        StringBuilder dataStr = new StringBuilder();
        BLL.Spreader bll = new BLL.Spreader();
        string userId = bll.GetSpreadIdByName(userName);
        MODEL.Spreader model = new MODEL.Spreader();
        model.SPASSWORD = EncrypterMD5.MD5.Md5(Request.Form["o"], 32);
        model.SUSERNAME = userName;
        bool flag = bll.SpreaderLogon(model.SUSERNAME, model.SPASSWORD);
        if (flag)
        {
            model.SPASSWORD = EncrypterMD5.MD5.Md5(Request.Form["n"], 32);
            model.SID = userId;
            flag = bll.SpreaderUpdatePassword(model);
            if (flag)
            {
                returnStr = "true";
            }
        }
        else
        {
            returnStr = "false";
        }
        return returnStr;
    }
    protected string LogonOut()
    {
        Session["SpreaderUsername"] = "";
        return "true";
    }
    protected string SpreadListByYear()
    {
        string userName = Session["SpreaderUsername"].ToString();
        StringBuilder htmlStr = new StringBuilder();
        BLL.Spreader bll = new BLL.Spreader();
        string userId = bll.GetSpreadIdByName(userName);
        BLL.SpreadMaster masterBll = new BLL.SpreadMaster();
        DataSet ds = masterBll.SpreadMasterList(userId);
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                htmlStr.Append("<tr id=\"" + dr[0].ToString() + "\">");
                htmlStr.Append("<td>" + dr[0].ToString() + "</td>");
                htmlStr.Append("<td>" + dr[2].ToString() + "</td>");
                htmlStr.Append("<td>" + dr[3].ToString() + "</td>");
                htmlStr.Append("<td>" + dr[4].ToString() + "</td>");
                htmlStr.Append("<td>" + dr[5].ToString() + "</td>");
                htmlStr.Append("<td><a class=\"link\" href=\"javascript:aaa(this)\">详细</a></td>");
                htmlStr.Append("</tr>");
            }
        }
        else
        {
            htmlStr.Append("<tr><td colspan=\"6\">暂无数据!</td></tr>");
        }
        return htmlStr.ToString();
    }
    protected string SpreadListByMonth()
    {
        string returnStr = string.Empty;
        string userName = Session["SpreaderUsername"].ToString();
        StringBuilder dataStr = new StringBuilder();
        BLL.Spreader bll = new BLL.Spreader();
        string userId = bll.GetSpreadIdByName(userName);
        MODEL.SpreadDeputy model = new MODEL.SpreadDeputy();
        model.SSPEARDID = userId;
        model.SYEAR = Request.Form["y"];
        model.SMONTH = Request.Form["m"];
        model.SDAY = Request.Form["d"];
        BLL.SpreadDeputy sdBll = new BLL.SpreadDeputy();
        DataSet ds = sdBll.SpreadDeputyList(model);
        cn.com.webxml.www.IpAddressSearchWebService ipName = new cn.com.webxml.www.IpAddressSearchWebService();
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                dataStr.Append("<tr>");
                dataStr.Append("<td>" + userName + "</td>");
                dataStr.Append("<td>" + ipName.getCountryCityByIp(dr["Sip"].ToString())[1] + "</td>");
                dataStr.Append("<td>" + dr["Syear"].ToString() + "</td>");
                dataStr.Append("<td>" + dr["Smonth"].ToString() + "</td>");
                dataStr.Append("<td>" + dr["Sday"].ToString() + "</td>");
                dataStr.Append("<td>" + Convert.ToDateTime(dr["Stime"]).ToShortTimeString() + "</td>");
                dataStr.Append("<td>" + GetType(dr["Stype"].ToString()) + "</td>");
                dataStr.Append("</tr>");
            }
        }
        else
        {
            dataStr.Append("<tr><td colspan=\"7\">暂无数据!</td></tr>");
        }
        return dataStr.ToString();
    }
    protected string GetUrl()
    {
        string returnStr = ConfigurationSettings.AppSettings["WebUrl"].ToString();
        returnStr += "r/" + Session["SpreaderUsername"].ToString() + ".html";
        return returnStr;
    }
    protected string GetType(string stateNum)
    {
        if (stateNum.Equals("0"))
        {
            return "注册用户";
        }
        else
        {
            return "新手卡";
        }
    }
}
